//
//  MyHeadInfoView.m
//  Rabbit
//
//  Created by 阿帕 on 2018/11/6.
//  Copyright © 2018年 GCY. All rights reserved.
//

#import "MyHeadInfoView.h"
#import "UIImage+CYCircle.h"

@interface MyHeadInfoView()

@property (strong , nonatomic)UIImageView *headImageView;

@property (strong , nonatomic)UILabel *nicknameLabel;

@property (strong , nonatomic)UILabel *accTypeLabel;

@property (strong , nonatomic)UIButton *registerBtn;

@end

@implementation MyHeadInfoView

#pragma mark - Intial
- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUpUI];
    }
    
    return self;
}

- (void)dealloc {
    [KNotificationCenter removeObserver:self name:LOGIN_LOGOUT object:nil];
}

- (void)setUpUI {
    WEAKSELF
    
    self.backgroundColor = [UIColor whiteColor];
    
    [KNotificationCenter addObserver:self selector:@selector(logoutEvent:) name:LOGIN_LOGOUT object:nil];
    
    //金额背景图片 - - - - - - - - - - - -  - - - - - - -- -
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, self.cy_height)];
//    UIColor *blueColor = [UIColor colorWithPatternImage:[UIImage gradientImageWithBounds:bgImageView.bounds andColors:@[[UIColor cy_colorWithHexString:@"#3076DE"], [UIColor cy_colorWithHexString:@"#4CA3F5"]] andGradientType:GradientDirectionLeftToRight]];
//    [bgImageView setBackgroundColor:blueColor];
    bgImageView.image = image(@"gcy_img_my_topBg");
    bgImageView.userInteractionEnabled = YES;
    [self addSubview:bgImageView];
    
    UILabel *grzxLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, StatusBarHeight, ScreenW, 50)];
    grzxLabel.text = @"我的";
    grzxLabel.textColor = [UIColor whiteColor];
    grzxLabel.font = [UIFont boldSystemFontOfSize:18];
    grzxLabel.textAlignment = NSTextAlignmentCenter;
    [bgImageView addSubview:grzxLabel];
    
    _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(35, 16 + NavHeight, 68, 68)];
    cut(_headImageView, _headImageView.cy_width/2);
    _headImageView.image = image(@"gcy_img_my_defultHead");
    _headImageView.tag = 10;
    _headImageView.userInteractionEnabled = YES;
    [_headImageView nyl_addTapGes:^{
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(MyHeadInfoBtnClick:)]) {
            [weakSelf.delegate MyHeadInfoBtnClick:_headImageView.tag];
        }
    }];
    [self addSubview:_headImageView];
    
    //昵称
    _nicknameLabel = [[UILabel alloc] initWithFrame:CGRectMake(_headImageView.cy_right + 10, _headImageView.cy_origin.y + 10, 150, 20)];
    _nicknameLabel.text = @"--";
    _nicknameLabel.textColor = [UIColor whiteColor];
    _nicknameLabel.font = font(18);
    _nicknameLabel.hidden = YES;
    [self addSubview:_nicknameLabel];
    
    //账号
    _accTypeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_headImageView.cy_right + 10, _nicknameLabel.cy_bottom + 5, 100, 20)];
    _accTypeLabel.font = font(13);
    _accTypeLabel.text = @"--";
    _accTypeLabel.textColor = [UIColor whiteColor];
    _accTypeLabel.adjustsFontSizeToFitWidth = YES;
    _accTypeLabel.hidden = YES;
    [self addSubview:_accTypeLabel];
    
    
    //登录注册
    _registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _registerBtn.tag = 11;
    _registerBtn.frame = CGRectMake(_headImageView.cy_right + 10, _headImageView.cy_origin.y + 20, 95, 30);
    [_registerBtn setTitle:@"登录/注册" forState:UIControlStateNormal];
    [_registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _registerBtn.titleLabel.font = font(16);
    _registerBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [_registerBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_registerBtn];
}

- (void)setUserInfoDic:(NSDictionary *)userInfoDic {
    
    _userInfoDic = userInfoDic;
    
    if ([DCSpeedy isBlankDictionary:_userInfoDic] == YES) {
        //未登录
        _headImageView.image = image(@"gcy_img_my_defultHead");
        _nicknameLabel.hidden = YES;
        _accTypeLabel.hidden = YES;
        _registerBtn.hidden = NO;
    }else {
        _nicknameLabel.hidden = NO;
        _accTypeLabel.hidden = NO;
        _registerBtn.hidden = YES;
        
        [_headImageView setImageWithURL:[NSURL URLWithString:userInfoDic[@"headImg"]] placeholder:image(@"gcy_img_my_defultHead")];
        _nicknameLabel.text = _userInfoDic[@"name"];
        _accTypeLabel.text = _userInfoDic[@"phone"];
    }
    
}

- (void)logoutEvent:(NSNotification *)notification {
    
//    _userInfoDic = notification.object;
    _headImageView.image = image(@"gcy_img_my_defultHead");
    _nicknameLabel.hidden = YES;
    _accTypeLabel.hidden = YES;
    _registerBtn.hidden = NO;
    
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
}

- (void)buttonClick:(UIButton *)button {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(MyHeadInfoBtnClick:)]) {
        [self.delegate MyHeadInfoBtnClick:button.tag];
    }
}

@end
